package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwddT;
import cn.e4j.modular.crm.mapper.Gwai790Mapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  季返报表 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-05-07
 */
@Service
public class Gwai790ServiceImpl extends ServiceImpl<Gwai790Mapper, GwddT> {

    @Autowired
    private SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Dto> getGwddlist(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        List<Dto> gwddDtos = this.baseMapper.getGwddlist(page, dto);
        return gwddDtos;
    }

    @Transactional(readOnly = true)
    public Dto getGwal(Dto dto) {
        List<Dto> list = this.baseMapper.getGwal(dto);
        Dto gwalDtos = list.size() > 0 ? list.get(0): null ;
        return gwalDtos;
    }

    @Transactional(readOnly = true)
    public Dto getMA041(Dto dto) {
        Dto maDtos = this.baseMapper.getMA041(dto);
        return maDtos;
    }

    @Transactional(readOnly = true)
    public Dto getSumTgi(Dto dto) {
        Dto outDto = Dtos.newDto();
        Dto tgDtos = this.baseMapper.getSumTg(dto);
        Dto tiDtos = this.baseMapper.getSumTi(dto);
        outDto.put("TG_SUM",tgDtos.getBigDecimal("TG_SUM"));
        outDto.put("TI_SUM",tiDtos.getBigDecimal("TI_SUM"));
        return outDto;
    }

    @Transactional(readOnly = true)
    public Dto getSumTk(Dto dto) {
        Dto tkDtos = this.baseMapper.getSumTk(dto);
        return tkDtos;
    }

    @Transactional(readOnly = true)
    public Dto getYQHK(Dto dto) {//逾期回款
        Dto outDto = Dtos.newDto();
        String DOCDATE_START = dto.getString("DOCDATE_START");
        String DOCDATE_END = dto.getString("DOCDATE_END");
        String DATE_START = DOCDATE_START.substring(0,6);
        String DATE_END = DOCDATE_END.substring(0,6);
        dto.put("DATE_START",DATE_START);
        dto.put("DATE_END",DATE_END);
        Dto ycyeDtos = this.baseMapper.getSumQc(dto);//月初余额
        Dto bqzdlsskDtos = this.baseMapper.getSumTb(dto);//本期针对历史的收款
        Dto bqzqwhxDtos = this.baseMapper.getSumTkWHX(dto);//本期帐期未核销收款
        BigDecimal YQHK = ycyeDtos.getBigDecimal("QC0211").subtract(bqzdlsskDtos.getBigDecimal("TB043"))
                .subtract(bqzqwhxDtos.getBigDecimal("TK032"));
        outDto.put("YQHK",YQHK);
        return outDto;
    }

    @Transactional(readOnly = true)
    public Dto getWFLFH(Dto dto) {//无返利发货
        Dto wflfhDtos = this.baseMapper.getSumWFLFH(dto);
        return wflfhDtos;
    }

    @Transactional(readOnly = true)
    public Dto getPH(Dto dto) {//铺货
        Dto outDto = Dtos.newDto();
        Dto phfhDtos = this.baseMapper.getSumPHFH(dto);
        Dto phhkDtos = this.baseMapper.getSumPHHK(dto);
        Dto phwflDtos = this.baseMapper.getSumPHWFL(dto);
        outDto.put("PHFH",phfhDtos.getBigDecimal("PHFH"));
        outDto.put("PHHK",phhkDtos.getBigDecimal("PHHK"));
        outDto.put("PHWFL",phwflDtos.getBigDecimal("PHWFL"));
        return outDto;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwdelist(Dto dto) {
        List<Dto> gwdeDtos = this.baseMapper.getGwdelist(dto);
        return gwdeDtos;
    }
}
